//
//  RecommondCityViewController.m
//  TravelToHelp
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 冯贵宾. All rights reserved.
//

#import "RecommondCityViewController.h"
#import "ReHotCityModel.h"
#import "CityTableViewCell.h"
#import "RecommendViewController.h"
#import "XiAndShouViewController.h"
#import "AmbitusListViewController.h"

@interface RecommondCityViewController ()<UITableViewDataSource, UITableViewDelegate>

// 主页tableView
@property (nonatomic, strong) UITableView *recomTableView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) DDIndicator *indicator;


@end

@implementation RecommondCityViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
 
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"推荐城市";
    self.navigationController.navigationBar.translucent = YES;
    [self addSubViews];
    [self setUpData];
//    [self addUpData];
    
}

// 添加视图
- (void)addSubViews
{
    // tableView
    self.recomTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:(UITableViewStylePlain)];
    // 代理
    self.recomTableView.dataSource = self;
    self.recomTableView.delegate = self;
    self.recomTableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.recomTableView];
    // 左边返回
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhuishouye"] style:(UIBarButtonItemStylePlain) target:self action:@selector(backLeftBar:)];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    
}

// 返回首页
- (void)backLeftBar:(UIBarButtonItem *)barButton
{
    [self.navigationController popViewControllerAnimated:YES];
}


// 解析数据
- (void)setUpData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"HotCityList" ofType:@"plist"];
    NSArray *plistArray = [NSArray arrayWithContentsOfFile:path];
    self.dataArray = [NSMutableArray array];
    for (NSDictionary *dic in plistArray) {
        ReHotCityModel *model = [[ReHotCityModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.dataArray addObject:model];
    }
}

#pragma mark -- UITableViewDataSource协议
// 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

// cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cityListCell";
    CityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[CityTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
    }
    if (self.dataArray.count > 0) {
        ReHotCityModel *model = self.dataArray[indexPath.row];
        cell.cityPhoto.image = [UIImage imageNamed:model.photo];
        cell.cityNameLB.text = model.cityName;
        cell.praiseLB.text = model.praise;
    }
    
    
    return cell;
}

// 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 203;
}

// 跳转界面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // 回调网址
    ReHotCityModel *model = self.dataArray[indexPath.row];
    NSString *url = model.url;
    NSString *seasonUrl = model.seasonUrl;
//    _cityBlock(url);

    if (indexPath.row > 2) {
        // 发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATIONONEHOTCITY" object:self userInfo:@{@"cityUrl":url}];
        // 发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATIONONEFUNSEASON" object:self userInfo:@{@"funSeason":seasonUrl}];
        
        [self.navigationController popViewControllerAnimated:YES];
    } else if (indexPath.row == 2) {
        AmbitusListViewController *aVC = [[AmbitusListViewController alloc] init];
        aVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:aVC animated:YES];
        
    } else {
        XiAndShouViewController *xiVC = [[XiAndShouViewController alloc] init];
        xiVC.detailsUrl = model.url;
        [self.navigationController pushViewController:xiVC animated:YES];
    }
}

@end
